﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text.RegularExpressions;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace CommunitySystem
{
    public partial class PeiXunMainWindow : ChildWindow
    {
        private PeiXunService.PeiXunServiceClient client1 = new PeiXunService.PeiXunServiceClient();
        private EmpbasicService.EmpBasicServiceClient Client = new EmpbasicService.EmpBasicServiceClient();
        private PeiXunService.PeiXunModel model = new PeiXunService.PeiXunModel();
        private int oldpiaoshu = 0;
        private int oldpiaoshu1 = 0;
        private int oldkeyong = 0;
        public bool IsNew = false;
        public PeiXunMainWindow(int id, bool isnew)
        {
            InitializeComponent();
            IsNew = isnew;
            tt.LostFocus += new RoutedEventHandler(tt_LostFocus);
            tt1.LostFocus += new RoutedEventHandler(tt1_LostFocus);
            client1.GetPeiXunModelCompleted += new EventHandler<PeiXunService.GetPeiXunModelCompletedEventArgs>(client1_GetPeiXunModelCompleted);
            client1.AddPeiXunModelCompleted += new EventHandler<PeiXunService.AddPeiXunModelCompletedEventArgs>(client1_AddPeiXunModelCompleted);
            client1.UpdatePeiXunModelCompleted += new EventHandler<PeiXunService.UpdatePeiXunModelCompletedEventArgs>(client1_UpdatePeiXunModelCompleted);
            if (isnew)
            {
                this.DataContext = model;
            }
            else
            {
                client1.GetPeiXunModelAsync(id);
            }
        }

        void client1_UpdatePeiXunModelCompleted(object sender, PeiXunService.UpdatePeiXunModelCompletedEventArgs e)
        {
            if (e.Result == true)
            {
                SilverlightMessageBoxLibrary.Message.InfoMessage("编辑成功！");
                this.DialogResult = true;
            }
        }

        void client1_AddPeiXunModelCompleted(object sender, PeiXunService.AddPeiXunModelCompletedEventArgs e)
        {
            if (e.Result == true)
            {
                SilverlightMessageBoxLibrary.Message.InfoMessage("新增成功！");
                this.DialogResult = true;
            }
        }

        void client1_GetPeiXunModelCompleted(object sender, PeiXunService.GetPeiXunModelCompletedEventArgs e)
        {
            if(e.Result != null && e.Result.Count > 0)
            {
                model = e.Result[0];
                oldpiaoshu = oldpiaoshu1 = model.PiaoShu;
                oldkeyong = model.PiaoShu - model.YuPiao;
            }
            this.DataContext = model;
        }

        private void CancelButton_Click(object sender, RoutedEventArgs e)
        {
            this.DialogResult = false;
        }

        private void OKButton_Click(object sender, RoutedEventArgs e)
        {
            if(model.EndTime < model.StartTime)
            {
                SilverlightMessageBoxLibrary.Message.ErrorMessage("请正确输入开始结束日期！");
                return;
            }
            if(model.XianZhiPiaoShu > model.PiaoShu)
            {
                SilverlightMessageBoxLibrary.Message.ErrorMessage("票数限制不能多于可预订票数！");
                return;
            }
            if (IsNew)
            {
                model.YuPiao = model.PiaoShu;
                client1.AddPeiXunModelAsync(model);
            }
            else
            {
                if (oldkeyong > model.PiaoShu)
                {
                    SilverlightMessageBoxLibrary.Message.ErrorMessage("已预定" + oldkeyong + "张票，多于培训可预订票数！");
                    return;
                }
                client1.UpdatePeiXunModelAsync(model);
            }
        }

        private string pattern = @"^[0-9]+([.]{1}[0-9]+){0,1}$";
        private void TextBox_TextChanged(object sender, TextChangedEventArgs e)
        {
            Match m = Regex.Match(((TextBox)sender).Text, pattern); // 匹配正则表达式
            if (!m.Success) // 输入的不是数字
            {
                SilverlightMessageBoxLibrary.Message.ErrorMessage("只能输入数字！");
                ((TextBox)sender).Text = "";
                return;
            }
            
        }
        void tt_LostFocus(object sender, RoutedEventArgs e)
        {
            if (!string.IsNullOrEmpty(((TextBox)sender).Text))
            {
                model.YuPiao = model.YuPiao + ((Convert.ToInt32(((TextBox)sender).Text)) - oldpiaoshu1);
                oldpiaoshu1 = (Convert.ToInt32(((TextBox) sender).Text));
            }
        }

        void tt1_LostFocus(object sender, RoutedEventArgs e)
        {
           
        }
    }
}
